namespace Brjnk.GpxExplorer.WPF
{
    using System;
    using System.Windows.Input;
    using Brjnk.GpxExplorer.ViewModel;
    using Brjnk.MVVM;

    /// <summary>
    /// </summary>
    public class SelectCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        protected void OnCanExecuteChanged()
        {
            if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty);
        }

        public void Execute(object parameter)
        {
            Select(parameter);

            var dataItem = parameter as DataItem;
            if (dataItem != null) Select(dataItem.Parent);
        }

        private void Select(object iSelectable)
        {
            var selectable = iSelectable as ISelectable;
            if (selectable == null) return;
            selectable.IsSelected = !selectable.IsSelected;
        }
    }
}